package main.metadata.parser;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import main.metadata.metadata.LianJiaHouse;
import main.metadata.metadata.LianJiaParams;
import main.monitor.URLPool;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import spider_bean.Baseinfo;
import spider_bean.BasicHouseInfo;
import spider_bean.ImageUrl;
import tool.FileOperation;
import util.net.NetUtils;

public class LianJiaDocParser {

	private static String houseListSelector = "ul#house-lst > li";

	public static List<Document> getDocsViaURLS(List<String> URLS) throws Exception{
		List<Document> docs = new ArrayList<Document>();
		for(String URL : URLS){
			Document doc = Jsoup.parse(NetUtils.httpGet(URL));
			docs.add(doc);
		}
		return docs;
	}

	public static List<LianJiaHouse> getHouseList(Document doc){
		List<LianJiaHouse> list = new ArrayList<LianJiaHouse>();
		Elements lis = doc.select(houseListSelector);
		for(Element li : lis){
			try{
				LianJiaHouse house = new LianJiaHouse();
				String houseId = li.attr("data-id");
				if(houseId == null || houseId.isEmpty())
					continue;
				String houseTitle = li.select("div.info-panel > h2 > a").attr("title");
				String houseURL = LianJiaParams.BaseURL + li.select("div.info-panel > h2 > a").attr("href");
				String regionURL = LianJiaParams.BaseURL + li.select("div.info-panel > div.col-1 > div.where > a").attr("href");
				String houseLocation = li.select("div.info-panel > div.col-1 > div.where > a > span").text();
				String houseRoom = li.select("div.info-panel > div.col-1 > div.where > span.zone > span").text();
				String houseArea = li.select("div.info-panel > div.col-1 > div.where > span.meters").text();
				String spanText = li.select("div.info-panel > div.col-1 > div.where > span").text();
				String[] spanTexts = spanText.split(" ");
				String houseDirection = spanTexts[spanTexts.length -1];
				String housePrice = li.select("div.info-panel > div.col-3 > div.price > span.num").text();
				String pricePerSquare = li.select("div.info-panel > div.col-3 > div.price-pre").text();
				Elements img = li.select("div.info-panel > div.col-3 > div.price > img");

				String[] otherInfos = li.select("div.info-panel > div.col-1 > div.other > div.con").text().split("/");
				String houseType = otherInfos[0];
				String houseHeight = otherInfos[1];
				String[] houseOtherStrs = otherInfos[2].split("年建");
				String houseBuildYear = houseOtherStrs[0];
				String houseBuildType = houseOtherStrs[1];

				boolean isDown = img.size() >0;

				house.setHouseId(houseId);
				house.setHouseTitle(houseTitle);
				house.setHouseLocation(houseLocation);
				house.setHouseRoom(houseRoom);
				house.setHouseArea(houseArea);
				house.setHouseDirection(houseDirection);
				house.setHousePrice(housePrice);
				house.setPricePerSquare(pricePerSquare);
				house.setDown(isDown);
				house.setHouseURL(houseURL);
				house.setRegionURL(regionURL);
				house.setHouseType(houseType);
				house.setHouseHeight(houseHeight);
				house.setHouseBuildType(houseBuildType);
				house.setHouseBuildYear(houseBuildYear);
				list.add(house);
			} catch (Exception e){
				e.printStackTrace();
			}

		}

		detectNextPage(doc);

		return list;
	}


	public static void detectNextPage(Document doc){
		Elements pageLinks = doc.select("div.page-box, div.house-lst-page-box");
		String pageDataStr = pageLinks.attr("page-data");
		String[] pageDatas = pageDataStr.split(",");
		if(pageDatas.length == 2){
			String totalPage = pageDatas[0].split(":")[1];
			String hrefTemplet = pageLinks.attr("page-url");
			String currentPage = pageDatas[1].split(":")[1].substring(0,pageDatas[1].split(":")[1].length()-1);
			if(!currentPage.equals(totalPage)){
				String nextPage = String.valueOf((Integer.valueOf(currentPage)+1));
				String nextURL = LianJiaParams.BaseURL + hrefTemplet.replace("{page}", nextPage);
				//				System.out.println("URL ADD:"+nextURL);
				URLPool.getInstance().pushURL(nextURL);
			}
		}

	}

	public static  String changeDivClassdata (String data)
	{
		return "div[class="+data+"]" ;
	}
	public static  String changeClassdata (String tag,String data)
	{
		return  tag+"[class="+data+"]" ;
	}

	public static Document getDocument(String data){

		Document doc = null;
		try {
			doc = Jsoup.parse(data);
			//			doc = Jsoup.parse(Jsoup.connect(url).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return doc;
	}

	public static String getHtmlString(String url){

		String doc = null;
		try {
			doc=	URLPool.getInstance().fakeHeader(url);
			//			doc = Jsoup.parse(Jsoup.connect(url).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}

	public static  BasicHouseInfo   ParseHouseData(Document doc){
		BasicHouseInfo info=new BasicHouseInfo();
		Elements overview = doc.select("div[class=overview]");  //doc.select("div[class=fanye gray6]")
		Elements content = overview.select("div[class=content]");

		//展示图片
		Elements el= doc.getElementsByClass("smallpic").select("li");
		List<ImageUrl> images= parserImageUrl(el);
		info.setImages(images);

		//价格
		Elements price= doc.getElementsByClass("price");
		info.setTotalprice(price.select(changeClassdata("span","total")).text()+price.select(changeClassdata("span","unit")).text());
		info.setUnitPriceValue(price.select(changeClassdata("div","unitPrice")).select(changeClassdata("span","unitPriceValue")).text());
		info.setShoufu(price.select(changeClassdata("span","taxtext")).text());

		//房屋信息
		Elements houseinfo = content.select("div[class=houseInfo]");
		Elements room=houseinfo.select(changeDivClassdata("room"));
		Elements type=houseinfo.select(changeDivClassdata("type"));
		Elements area=houseinfo.select(changeDivClassdata("area"));
		info.setRoom_maininfo(room.select(changeDivClassdata("mainInfo")).text());
		info.setRoom_subInfo (room.select(changeDivClassdata("subInfo")).text());
		info.setType_maininfo(type.select(changeDivClassdata("mainInfo")).text());
		info.setType_subInfo (type.select(changeDivClassdata("subInfo")).text());
		info.setArea_maininfo(area.select(changeDivClassdata("mainInfo")).text());
		info.setArea_subInfo (area.select(changeDivClassdata("subInfo")).text());

		//小区名称 位置
		//			 aroundInfo
		Elements  aroundInfo=content.select(changeClassdata("div","aroundInfo"));
		//			 System.out.println("aroundInfo:"+aroundInfo.toString());
		//			 Elements  communityName=aroundInfo.select(changeClassdata("div","communityName")).select("a");
		//			 
		//			 System.out.println("communityName:"+communityName.text().replace(" 地图", ""));

		info.setCommunityName(aroundInfo.select(changeClassdata("div","communityName")).text().replace("地图", "").replace("小区名称", ""));
		info.setAreaName(aroundInfo.select(changeClassdata("div","areaName")).text() );
		info.setVisitTime(aroundInfo.select(changeClassdata("div","visitTime")).text() );
		info.setHouseRecord(aroundInfo.select(changeClassdata("div","houseRecord")).text().replace("举报", ""));

		//			 System.out.println("m-content:"+doc.select("div[class=m-content]")); 

		//基本属性
		Elements mcontent = doc
				.select("div[class=m-content]")
				.select("div[class=box-l]")
				.select("div[class=newwrap baseinform]")
				.select("div[class=content]");

	Baseinfo baseinfo=	 parserBaseInfo(mcontent);
		
		info.setBaseInfos(baseinfo);
		
//		System.out.println("m-content:"+mcontent.toString()); 
	
		


		return info;

	}
	public static Baseinfo  parserBaseInfo(Elements mcontent){
		Baseinfo base=new Baseinfo();
		Elements lis=mcontent.select("li");
		System.out.println("m-content:"+ lis.size()); 
		for (int i=0;i<lis.size();i++) {
//			System.out.println("m-content:"+element.text()); 
			base.setHousetype (lis.get(0).text());                      // 房屋户型</span>3室2厅1厨1卫</li> 
			base.setFloor (lis.get(1).text());                        // 所在楼层</span>高楼层 (共9层)</li> 
			base.setCoveredarea(lis.get(2).text());            // 建筑面积</span>103.53㎡</li> 
			base.setFamilystructure (lis.get(3).text());                // 户型结构</span>暂无数据</li> 
			base.setJacketarea(lis.get(4).text());                     // 套内面积</span>暂无数据</li> 
			base.setStructuretype(lis.get(5).text());                  // 建筑类型</span>板塔结合</li> 
			base.setHouseorientation(lis.get(6).text());               // 房屋朝向</span>南 北</li> 
			base.setBuildingstructure(lis.get(7).text());              // 建筑结构</span>砖混结构</li> 
			base.setDecorationsituation(lis.get(8).text());           // 装修情况</span>精装</li> 
			base.setProportionhouseholds(lis.get(9).text());           // 梯户比例</span>一梯两户</li> 
			base.setWithelevator(lis.get(10).text());                  // 配备电梯</span>暂无数据</li> 
			base.setPropertyright(lis.get(11).text());                 // 产权年限</span>70年</li> 
			base.setListingtime(lis.get(12).text());                     //	挂牌时间</span>2016-09-17</li> 
			base.setTransactionownership(lis.get(13).text());           //	交易权属</span>商品房</li> 
			base.setLasttradetime(lis.get(14).text());                  //	上次交易</span>2011-02-09</li> 
			base.setHousinguse(lis.get(15).text());                     //	房屋用途</span>普通住宅</li> 
		    base.setHouselife(lis.get(16).text());                     //	房屋年限</span>满五年</li> 
		    base.setOwnership(lis.get(17).text());                     //	产权所属</span>非共有</li> 
//		    base.setIsunique(lis.get(18).text());                     //  是否唯一</span>唯一住宅</li>--> 
		    base.setMortgageinformation(lis.get(18).text());           //	抵押信息</span><span style="display:inline-block(lis.get(0).text());  width:64%(lis.get(0).text());  white-space:nowrap(lis.get(0).text());  overflow:hidden(lis.get(0).text());  text-overflow:ellipsis(lis.get(0).text());  vertical-align:middle(lis.get(0).text());  " title="有抵押 20万元 公积金管理中心 业主自还">有抵押 20万元 公积金管理中心 业主自还</span></li> 
		    base.setRoomspareparts(lis.get(19).text());                     //	房本备件</span>已上传房本照片</li> 
		}
//		   base.setIsunique(mcontent.select("!--<li").text());
		
		return base;
		
	}
	public static List<ImageUrl>   parserImageUrl(Elements data){
		List<ImageUrl>  lists=new ArrayList<ImageUrl>();

		for (Element element : data) {
			ImageUrl url=new ImageUrl();
			url.setData_url(element.attr("data-pic").toString());
			url.setData_size(element.attr("data-size").toString());
			url.setData_desc(element.attr("data-desc").toString());
			lists.add(url);
		}
		return lists;
	} 
 public static List<ImageUrl>   parserBaseinfo(Elements data){
		List<ImageUrl>  lists=new ArrayList<ImageUrl>();

		for (Element element : data) {
			ImageUrl url=new ImageUrl();
			url.setData_url(element.attr("data-pic").toString());
			url.setData_size(element.attr("data-size").toString());
			url.setData_desc(element.attr("data-desc").toString());
			lists.add(url);
		}
		return lists;
	} 



	public static void main(String[] args) throws Exception{
		String url ="https://nj.lianjia.com/ershoufang/103101633005.html";
				String htmldata=getHtmlString(url);
//		String htmldata=	FileOperation.readTxtContent("D:\\789.txt");
		Document doc=getDocument(htmldata);
		BasicHouseInfo info=	ParseHouseData(doc);
		System.out.println(info.getArea_maininfo());
		System.out.println(info.getArea_subInfo() );
		System.out.println(info.getRoom_maininfo());
		System.out.println(info.getRoom_subInfo());
		System.out.println(info.getTotalprice());  //    "price " select(changeDivClassdata("total")).toString()
		System.out.println(info.getUnitPriceValue()); 
		System.out.println(info.getShoufu()); 
		List<ImageUrl>  lists=info.getImages();
		for (ImageUrl element : lists) {
			System.out.println(element.getData_url()); 
			System.out.println(element.getData_size()); 
			System.out.println(element.getData_desc()); 
		}


		System.out.println( info.getCommunityName());
		System.out.println( info.getAreaName     ());
		System.out.println( info.getVisitTime    ());
		System.out.println( info.getHouseRecord  ());

	}


}
